﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;


namespace MLY._404
{
    public partial class _04 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UUpsTitle.Text = Resources.Resource.P404_UUps;

            string ErrorPath = "";
            
            Exception currExp = (Exception)Application["currError"];

            if (Request.QueryString["aspxerrorpath"] != null)
            {
                ErrorPath = Request.QueryString["aspxerrorpath"];
            }
            if (ErrorPath != "/LTR/undefined")
            {
                string ExceptionMessage = "";
                string user = "";
                string errMsg = "";
                string stacktrace = "";

                if (currExp != null)
                {
                    ExceptionMessage = currExp.Message;
                    stacktrace = currExp.StackTrace;
                }

                if (Session["FullName"] != null)
                {
                    user = Session["FullName"].ToString();
                }
                

                errMsg = "<u>Error caught in  MLY system.</u><br/> StackTrace: " + stacktrace +
                         "<br/> Message: " + ExceptionMessage +
                         "<br/> " + "Url: " + ErrorPath +
                         "<br/> " + "User: " + user;
                Application.Remove("currError");

//                errMsg = String.Format(@"Error Caught in Application_Error MLY : <br/>  <br/> StackTrace:{0}  <br/>  <br/>  Message:{1}  <br/>  <br/> 
//                                 Exception message:{2}  <br/>  <br/> ", stacktrace, errMsg, ExceptionMessage);

                
                // NOTE: Investigating a bug. Too many times 404 page is activated. 
                // Thus, for now, only writing log message and not sending emails with errors,
                
                //Utils.mlySendMail("Error Handeled at MLY", "yoav@mly.co.il", "", "pastch@gmail.com", errMsg, false, null);
                Log.WriteError(errMsg);
               
                // EOF NOTE.
            }
        }

        public static CultureInfo ResolveCulture()
        {
            string[] languages = HttpContext.Current.Request.UserLanguages;
            if (languages == null || languages.Length == 0)
                return null;
            try
            {
                string language = languages[0].ToLowerInvariant().Trim();
                return System.Globalization.CultureInfo.CreateSpecificCulture(language);
            }
            catch (ArgumentException)
            {
                return null;
            }
        }

        protected override void InitializeCulture()
        {
            CultureInfo cult = ResolveCulture();
            Thread.CurrentThread.CurrentUICulture = cult;
            Thread.CurrentThread.CurrentCulture = cult;
            base.InitializeCulture();
        }
    }
}
